<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>天天生鲜-购物车</title>
	<link rel="stylesheet" type="text/css" href="static/front/css/reset.css">
	<link rel="stylesheet" type="text/css" href="static/front/css/main.css">
</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到天天生鲜!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em>张 山</em>
				</div>
				<div class="login_btn fl">
					<a href=""><em>${user.username}</em></a>
					<span>|</span>
					<a href="register.html">注册</a>
				</div>
				<div class="user_link fl">
					<span>|</span>
					<a href="user_center_info.html">用户中心</a>
					<span>|</span>
					<a href="cart.html">我的购物车</a>
					<span>|</span>
					<a href="user_center_order.html">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="index.html" class="logo fl"><img src="static/front/images/logo.png"></a>
		<div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;购物车</div>
		<div class="search_con fr">
			<input type="text" class="input_text fl" name="" placeholder="搜索商品">
			<input type="button" class="input_btn fr" name="" value="搜索">
		</div>		
	</div>

	<div class="total_count">全部商品<em>5</em>件</div>	
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
	
	<c:forEach items="${list}" var="cart">
		<ul class="cart_list_td clearfix" id="cartItem${cart.id}">
			<li class="col01">
				<c:if test="${cart.checked==1}">
					<input id="checkbox${cart.id}" onclick="updateCheckStatus(${cart.id})" type="checkbox" name="selectCheckbox" checked="checked">
				</c:if>
				<c:if test="${cart.checked!=1}">
					<input id="checkbox${cart.id}" onclick="updateCheckStatus(${cart.id})" type="checkbox" name="selectCheckbox" >
				</c:if>
			</li>
			<li class="col02"><img src="${cart.product.mainImageFullUrl}"></li>
			<li class="col03">${cart.product.name}<br></li>
			<li class="col05">${cart.product.price}元</li>
			<li class="col06">
				<div class="num_add">
					<a onclick="updateQuantity(${cart.id},1,${cart.product.price})" href="javascript:;" class="add fl">+</a>
					<input id="num${cart.id}" type="text" class="num_show fl" value="${cart.quantity}">	
					<a onclick="updateQuantity(${cart.id},-1,${cart.product.price})" href="javascript:;" class="minus fl">-</a>	
				</div>
			</li>
			<li class="col07" id="cartItemTotalPrice${cart.id}">${cart.product.price*cart.quantity}</li>
			<li class="col08"><a onclick="deleteById(${cart.id})" href="javascript:;">删除</a></li>
		</ul>
	</c:forEach>

	
	

	<ul class="settlements">
		<li class="col01"><input type="checkbox" name="" checked=""></li>
		<li class="col02">全选</li>
		<li class="col03">合计(不含运费)：<em id="totalPrice"></em><span>¥</span><br>共计<b><em id="totalProduct"></em></b>件商品</li>
		<li class="col04"><a href="${path}/order/getOrderConfirmPage.shtml">去结算</a></li>
	</ul>

	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>
	
	<script src="static/lib/jquery-1.11.3.js"></script>
	<script src="static/lib/mylayer.js"></script>
	<script src="static/lib/util.js"></script>
	<script src="static/lib/layui/layui.js"></script>
	<script type="text/javascript">
		layui.use(['layer'], function(){
		  var layer = layui.layer;
		});
		
		$(function() {
			refreshTotalPrice();
		});
		
		// 当前页面加载完了之后，计算选中商品的总价格
		function refreshTotalPrice() {
			var checkboxs = $("input[name=selectCheckbox]:checked");
			var totalPrice = 0;
			$(checkboxs).each(function(){
				// this代表当前正在遍历的checkbox的dom对象
				// checkbox的id="checkbox23"
				// this.id    checkbox23
				// substring
				var checkboxId = this.id;
				var cartId = checkboxId.substr("checkbox".length);
				var cartItemTotalPrice = $("#cartItemTotalPrice" + cartId).html();
				totalPrice += parseFloat(cartItemTotalPrice);
			});
			$("#totalPrice").html(totalPrice);
		}
	
		function refreshTotalProduct() {
			var checked = $("#checkbox" + cartId).prop("checked");
			var totalProduct = 0;
			$(checkboxs).each(function(){
			if(checked ==1){
				totalProduct++ ;
			}
			});
			$("#totalProduct").html(totalProduct);
		}
		
		function updateCheckStatus(cartId) {
			var checked = $("#checkbox" + cartId).prop("checked");// true/false
			if(checked) {
				checked = 1;
			} else {
				checked = 0;
			}
			$.post(
				"${path}/cart/updateCheckStatus.shtml",
				{"cartId": cartId, "checked" : checked},
				function(jsonObj) {
					if(jsonObj.code == util.SUCCESS) {
						mylayer.successMsg(jsonObj.msg);
						refreshTotalPrice();
					} else {
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			);
		}
		
		
		function updateQuantity(cartId, num, price) {
			$.post(
				"${path}/cart/updateQuantity.shtml",
				{"cartId" : cartId, "num" : num},
				function(jsonObj) {
					if(jsonObj.code == util.SUCCESS) {
						mylayer.successMsg(jsonObj.msg);
						// 更新显示的购买数量
						var quantity = parseInt($("#num" + cartId).val());
						quantity += num;
						$("#num" + cartId).val(quantity);
						// 更新购物车中当前这个商品数量*价格
						var totalPrice = quantity * price;
						$("#cartItemTotalPrice" + cartId).html(totalPrice);
						refreshTotalPrice();
					} else {
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			);
		}
		
		function deleteById(cartId) {
			layer.confirm("您确定要删除么？", function() {
				$.post(
					"${path}/cart/deleteById.shtml",
					{"cartId": cartId},
					function(jsonObj) {
						if(jsonObj.code == util.SUCCESS) {
							mylayer.successMsg(jsonObj.msg);
							$("#cartItem" + cartId).remove();
							refreshTotalPrice();
						} else {
							mylayer.errorMsg(jsonObj.msg);
						}
					}
				);
			});
		}
	</script>
</body>
</html>